package com.yq.cp.service.data.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.cp.entity.basics.EquipmentManagement;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.data.NengKongData;
import com.yq.cp.entity.data.NkData;
import com.yq.cp.entity.data.NkDataDetails;
import com.yq.cp.entity.data.vo.NkDataVo;
import com.yq.cp.entity.data.vo.SjcjVo;
import com.yq.cp.entity.data.vo.SjcjXqVo;
import com.yq.cp.mapper.basics.EquipmentManagementMapper;
import com.yq.cp.mapper.basics.OrganizationalStructureMapper;
import com.yq.cp.mapper.data.NkDataDetailsMapper;
import com.yq.cp.mapper.data.NkDataMapper;
import com.yq.cp.service.data.NkDataService;
import com.yq.cp.utils.DateTimeUtil;
import com.yq.cp.utils.TimerUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class NkDataServiceImpl implements NkDataService {

    @Resource
    NkDataMapper nkDataMapper;
    @Resource
    NkDataDetailsMapper nkDataDetailsMapper;
    @Resource
    OrganizationalStructureMapper organizationalStructureMapper;
    @Resource
    EquipmentManagementMapper equipmentManagementMapper;
    @Override
    public void add(List<NkDataVo> list) {
        for (int i = 0; i < list.size(); i++) {
            NkDataVo n = list.get(i);
            NkData nkData=new NkData();
            nkData.setDataId(n.getId());
            nkData.setSerialNo(n.getSerialNo());
            nkDataMapper.insert(nkData);
            List<NkDataDetails> NkDataDeatisls = n.getData();
            for (int j = 0; j < NkDataDeatisls.size(); j++) {
                NkDataDetails nkDataDetails = NkDataDeatisls.get(j);
                nkDataDetails.setNkDataId(nkData.getId());
                nkDataDetailsMapper.insert(nkDataDetails);
            }
        }

    }

    @Override
    public void addNeet(SjcjVo vo) {
        QueryWrapper<OrganizationalStructure> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("code1",vo.getSiteInfo());
        queryWrapper.eq("examine",1);
        List<OrganizationalStructure> organizationalStructures = organizationalStructureMapper.selectList(queryWrapper);
        if (organizationalStructures.size()>0){
            OrganizationalStructure organizationalStructure = organizationalStructures.get(0);
//            QueryWrapper<EquipmentManagement> equipmentManagementQueryWrapper=new QueryWrapper<>();
//            equipmentManagementQueryWrapper.eq("site_id",organizationalStructure.getId());
//            List<EquipmentManagement> equipmentManagements = equipmentManagementMapper.selectList(equipmentManagementQueryWrapper);


            List<SjcjXqVo> eleMeters = vo.getEle_meters();
            for (int i = 0; i < eleMeters.size(); i++) {
                SjcjXqVo sjcjXqVo = eleMeters.get(i);
                QueryWrapper<EquipmentManagement> queryWrapper1=new QueryWrapper<>();
                queryWrapper1.eq("sn",sjcjXqVo.getID());
                queryWrapper1.eq("site_id",organizationalStructure.getId());
                EquipmentManagement equipmentManagement = equipmentManagementMapper.selectOne(queryWrapper1);
                if (equipmentManagement==null){
                    continue;
                }
                equipmentManagement.setTotal(sjcjXqVo.getTotal());
                equipmentManagement.setTip(sjcjXqVo.getTip());
                equipmentManagement.setPeak(sjcjXqVo.getPeak());
                equipmentManagement.setNormal(sjcjXqVo.getNormal());
                equipmentManagement.setValley(sjcjXqVo.getValley());

                equipmentManagement.setReTotal(sjcjXqVo.getRe_Total());
                equipmentManagement.setReTip(sjcjXqVo.getRe_Tip());
                equipmentManagement.setRePeak(sjcjXqVo.getRe_Tip());
                equipmentManagement.setReNormal(sjcjXqVo.getRe_Normal());
                equipmentManagement.setReValley(sjcjXqVo.getRe_Valley());
                equipmentManagement.setOperationState(0);
                equipmentManagementMapper.updateById(equipmentManagement);
            }

            List<SjcjXqVo> virtualMeters = vo.getVirtual_meters();
            for (int i = 0; i < virtualMeters.size(); i++) {
                SjcjXqVo sjcjXqVo = virtualMeters.get(i);
                BigDecimal total = sjcjXqVo.getTotal();
                BigDecimal tip = sjcjXqVo.getTip();
                BigDecimal peak = sjcjXqVo.getPeak();
                BigDecimal normal = sjcjXqVo.getNormal();
                BigDecimal valley = sjcjXqVo.getValley();
                String name="";
                if (sjcjXqVo.getTagNO()==null){
                    return;
                }
                if (  sjcjXqVo.getTagNO().equals("production")){//发电
                    name="发";
                    String name1="ModBus.逆变器";
                    String name2=".当日发电量01kWh";
                    this.add(organizationalStructure.getCode(),null,name1+name2,total);
                }else if (sjcjXqVo.getTagNO().equals("up")){//上网
                    name="上网";
                }else if (sjcjXqVo.getTagNO().equals("down")){//下网
                    name="下网";
                }else if (sjcjXqVo.getTagNO().equals("local")){//消纳
                    name="消网";
                }
                if (!name.equals("")){
                    this.add(organizationalStructure.getCode(),sjcjXqVo.getTagNO(),name+"电量总",total);
                    this.add(organizationalStructure.getCode(),sjcjXqVo.getTagNO(),name+"电量尖",tip);
                    this.add(organizationalStructure.getCode(),sjcjXqVo.getTagNO(),name+"电量峰",peak);
                    this.add(organizationalStructure.getCode(),sjcjXqVo.getTagNO(),name+"电量平",normal);
                    this.add(organizationalStructure.getCode(),sjcjXqVo.getTagNO(),name+"电量谷",valley);
                }
            }

            if (vo.getEnv_meters()!=null && vo.getEnv_meters().size()>0){
                SjcjXqVo sjcjXqVo = vo.getEnv_meters().get(0);
                organizationalStructure.setTemperature(sjcjXqVo.getTemperature());
                organizationalStructure.setHumidity(sjcjXqVo.getHumidity());
                organizationalStructure.setPm25(sjcjXqVo.getPM25());
                organizationalStructure.setPm10(sjcjXqVo.getPM10());
                organizationalStructure.setIlluminance(sjcjXqVo.getIlluminance());
                organizationalStructure.setIrradiation(sjcjXqVo.getIrradiation());
                organizationalStructureMapper.updateById(organizationalStructure);
            }
        }
    }

    private void add(String energyCode, String macCode, String name, BigDecimal gl){
        if (gl.compareTo(new BigDecimal(0))==-1){
            return;
        }
//        if (gl.compareTo(new BigDecimal(1000000))==1){
//            return;
//        }
        try{
            NengKongData glData=new NengKongData();
            glData.setName(name);
            glData.setValue(gl);
            glData.setProduceTime(DateTimeUtil.dqsj());
            glData.setEnergyCode(energyCode);
//            glData.setDId(System.currentTimeMillis());
            glData.setMacCode(macCode);
            glData.setHourTime(DateTimeUtil.dqsj());
            TimerUtil.list.add(glData);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
